package cn.devzyh.service.impl;

import cn.devzyh.dao.UserMapper;
import cn.devzyh.domain.*;
import cn.devzyh.service.UserService;
import cn.devzyh.utils.Md5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo findByConditionAndPage(UserVo userVo) {

        // 指定分页条件
        PageHelper.startPage(userVo.getCurrentPage(), userVo.getPageSize());

        // 查询用户数据
        List<User> users = userMapper.findByCondition(userVo);

        // 提取分页数据
        PageInfo<User> pageInfo = new PageInfo<>(users);

        return pageInfo;
    }

    @Override
    public void updateUserStatus(Integer id, String status) {

        User user = new User();
        user.setId(id);
        user.setStatus(status);
        user.setUpdateTime(new Date());

        userMapper.updateUserStatus(user);
    }

    @Override
    public User login(User user) {

        // 查询数据库数据
        User realUser = userMapper.login(user);
        if (null == realUser) {
            return null;
        }

        // 验证MD5密码信息
        boolean b = Md5Util.verify(user.getPassword(), realUser.getPassword(), null);
        if (b) {
            return realUser;
        } else {
            return null;
        }
    }

    @Override
    public List<Role> findUserRoleById(Integer userId) {

        return userMapper.findUserRoleById(userId);
    }

    @Override
    @Transactional
    public void userContextRole(UserVo userVo) {

        // 清除历史数据
        userMapper.deleteUserRoleRelationById(userVo.getUserId());

        // 添加新数据
        Date now = new Date();
        for (Integer roleId : userVo.getRoleIdList()) {

            UserRoleRelation relation = new UserRoleRelation();
            relation.setUserId(userVo.getUserId());
            relation.setRoleId(roleId);
            relation.setCreatedTime(now);
            relation.setUpdatedTime(now);
            relation.setCreatedBy("system");
            relation.setUpdatedBy("system");
            userMapper.saveUserRoleRelation(relation);
        }
    }

    @Override
    public Map<String, Object> getUserPermissions(Integer userId) {

        // 查询父级菜单信息
        List<Menu> menuList = userMapper.findMenuByUserIdAndPid(userId, -1);

        // 查询对应子级菜单
        for (Menu menu : menuList) {

            List<Menu> list = userMapper.findMenuByUserIdAndPid(userId, menu.getId());
            menu.setSubMenuList(list);
        }

        // 查询授权资源信息
        List<Resource> resourceList = userMapper.findResourceByUserId(userId);

        // 封装数据
        Map<String, Object> map = new HashMap<>();
        map.put("menuList", menuList);
        map.put("resourceList", resourceList);

        return map;
    }
}
